package com.cortex.carambola.view;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import com.cortex.animation.AnimationInOut;
import com.cortex.animation.CortexAnimatin;
import com.cortex.carambola.view.CortexRowSearch.ISlidFinger;
import com.cortex.carambolasummary.R;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.ViewFlipper;


public class HeaderView extends RelativeLayout implements OnClickListener{

	private OnActionHeaderListener onActionHeaderListener;
	private CortexRowSearch rowSearch;
	private ImageView imgLogo,imgForward,imgBack,imgRefresh,
	imgSeasonSummaryList,imgSummarySucceed;
	private ViewFlipper flipperLogo;

	public HeaderView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public HeaderView(Context context) {
		super(context);
	}
	@Override
	protected void onFinishInflate() {
		// TODO Auto-generated method stub
		super.onFinishInflate();
		init();
	}
	public void setOnActionHeaderListener(
			OnActionHeaderListener onActionHeaderListener) {
		this.onActionHeaderListener = onActionHeaderListener;
	}
	private void init(){
		imgLogo = (ImageView)findViewById(R.id.imgLogo);
		rowSearch = (CortexRowSearch)findViewById(R.id.rowSearch);
		imgForward = (ImageView)findViewById(R.id.imgForward);
		imgBack = (ImageView)findViewById(R.id.imgBack);
		imgRefresh = (ImageView)findViewById(R.id.imgRefresh);
		imgSeasonSummaryList = (ImageView)findViewById(R.id.imgSeasonSummaryList);
		flipperLogo = (ViewFlipper)findViewById(R.id.flipperLogo);
		imgSummarySucceed = (ImageView)findViewById(R.id.imgSummarySuccess);
		initHandlers();

		AnimationInOut animation = CortexAnimatin.createAnimation(imgLogo,true, 
				R.anim.push_right_in, R.anim.push_left_out);
		flipperLogo.setInAnimation(animation.getIn());
		flipperLogo.setOutAnimation(animation.getOut());
	}
	private void initHandlers(){
		imgLogo.setOnClickListener(this);
		imgBack.setOnClickListener(this);
		imgForward.setOnClickListener(this);
		imgRefresh.setOnClickListener(this);
		imgSeasonSummaryList.setOnClickListener(this);
		// listener to slid right or slid left of row search
		rowSearch.setOnSlidFingerListener(new ISlidFinger() {

			@Override
			public void toRight() {
				sendMessageToListener(ActionOnHeader.ROW_SEARCH_TO_RIGHT);
			}

			@Override
			public void toLeft() {
				sendMessageToListener(ActionOnHeader.ROW_SEARCH_TO_LEFT);
			}
		});
	}
	@Override
	public void onClick(View v) {
		if(v == imgLogo){
			sendMessageToListener(ActionOnHeader.LOGO);
		}
		else if(v == imgForward){
			sendMessageToListener(ActionOnHeader.FORWARD);
		}
		else if(v == imgBack){
			sendMessageToListener(ActionOnHeader.BACK);
		}
		else if(v == imgRefresh){
			sendMessageToListener(ActionOnHeader.REFRESH);
		}
		else if(v == imgSeasonSummaryList){
			sendMessageToListener(ActionOnHeader.SEASON_SUMMRIES);
		}
	}
	private void sendMessageToListener(ActionOnHeader action){
		if(onActionHeaderListener != null){
			onActionHeaderListener.onAction(action);
		}
	}
	public void setClickableBack(boolean isCanGoBack) {
		imgBack.setBackgroundResource(isCanGoBack?R.drawable.back_on:R.drawable.back_off);
		imgBack.setClickable(isCanGoBack);
	}
	public void setClickableForward(boolean isCanForward) {
		imgForward.setBackgroundResource(isCanForward?R.drawable.forword_on:R.drawable.forword_off);
		imgForward.setClickable(isCanForward);
	}
	public void setRowSearchText(String text) {
		rowSearch.setText(text);
	}
	public String getRowSearchText(){
		return rowSearch.getValue();
	}
	public void showProgressBar(){
		flipperLogo.setDisplayedChild(1);
	}
	public void hideProgressBar(){
		flipperLogo.setDisplayedChild(0);
	}
	public void showSummarySucceed(boolean isSucceed){
		if(isSucceed){
			imgSummarySucceed.setBackgroundResource(R.drawable.go_summray);
		}
		else{
			imgSummarySucceed.setBackgroundResource(R.drawable.hide_more_options);
		}
		List<Animation> animations = CortexAnimatin.createInOutAlpha(0.0f, 1.0f, 1000,1000);
		CortexAnimatin.startAnimations(imgSummarySucceed, animations.toArray(new Animation[animations.size()]));
	}
	public interface OnActionHeaderListener{
		void onAction(ActionOnHeader action);
	}
	public enum ActionOnHeader{
		LOGO,
		ROW_SEARCH_TO_RIGHT,
		ROW_SEARCH_TO_LEFT,
		FORWARD,
		BACK,
		REFRESH,
		SEASON_SUMMRIES
	}
	public void clearRowSearch() {
		rowSearch.clear();
	}


}
